drb56625 2018-10-25 22:57
浏览 65

无需刷新即可加载XML数据

I've script which get some values from XML record.

There's code:

<?php

//Data
$xml_data = '<image_process_call><image_url>https://i.pinimg.com/originals/e4/41/54/e44154308e3466d987665c6d50887f06.jpg</image_url><methods_list><method><name>collage</name><params>template_name=Nun Face in Hole;</params></method></methods_list><result_format>jpg</result_format><result_size>800</result_size><template_watermark>false</template_watermark></image_process_call>';

//Settings
$app_id = '';
$key = '';
$sign_data = hash_hmac('SHA1', $xml_data, $key);

//Send request
$request_url = 'http://opeapi.ws.pho.to/addtask?data='. $xml_data .'&sign_data='. $sign_data .'&app_id='. $app_id;
$request_xml = simplexml_load_file($request_url);
$request_id = strval($request_xml -> request_id);

if (isset($request_id)) {
    $result_url = 'http://opeapi.ws.pho.to/getresult?request_id='. $request_id;
    sleep(6);
    $result_xml = simplexml_load_file($result_url);
    $result_status = strval($result_xml -> status);
    $result_img = strval($result_xml -> result_url);
    if (isset($result_img)) {
        echo $result_img;
    } else {
        echo 'Result image not found';
    }
} else {
    echo 'Request ID not found';
}

?>

The problem depends on time to generate the second XML file. $result_xml took few seconds so I have to use sleep(6) function. If I remove this, I need to refresh the page (minimum three times) to get a link to generated image from second XML.

Do you have an idea how to do it more professionally? I can't be sure that every image will be generated in 6 seconds (sometimes shorter sometimes longer). Is there any method for genereting the result only after receiving $result_img? Thanks in advance for your help!

I think it is worth writing. In practice, it looks like this: Script does $request_xml and XML from site return:

<image_process_response>
<request_id>2d8d4dec-4344-4df0-a1e1-0c8df304ad11</request_id>
<status>OK</status>
<description/>
<err_code>0</err_code>
</image_process_response>

Script gets request_id from this XML and do $result_xml. However, this is XML and script doesn't get image's url immediately. It needs to wait a few seconds. After three times refreshing the page or using sleep(6) function finally we get:

<image_process_response>
<request_id>2d8d4dec-4344-4df0-a1e1-0c8df304ad11</request_id>
<status>OK</status>
<result_url>
http://worker-images.ws.pho.to/i1/9F1E2EAF-5B31-4407-8779-9A85F35862D3.jpg
</result_url>
<result_url_alt>
http://worker-images.ws.pho.to.s3.amazonaws.com/i1/9F1E2EAF-5B31-4407-8779-9A85F35862D3.jpg
</result_url_alt>
<limited_image_url>
http://worker-images.ws.pho.to/i1/3F797C83-2C2E-401C-B4AF-C4D36BBD442D.jpg
</limited_image_url>
<nowm_image_url>
http://worker-images.ws.pho.to/i1/9F1E2EAF-5B31-4407-8779-9A85F35862D3.jpg
</nowm_image_url>
<duration>2950.879097ms</duration>
<total_duration>2956.124067ms</total_duration>
</image_process_response>

Edit:

After trying to immediately generate the image I get such an XML:

<image_process_response>
<request_id>e615f0a1-ddee-4d81-94c4-a392f8f123e8</request_id>
<status>InProgress</status>
<description>The task is in progress, you need to wait for sometime.</description>
</image_process_response>

So this is reason why I see blank page...

Do someone have an idea how to force a script to reconnect with the second XML until it finds a result_url?

  • 写回答

1条回答 默认 最新

  • douyan1244 2018-10-26 01:31
    关注

    The problem depends on time to generate the second XML file. $result_xml took few seconds so I have to use sleep(6) function. If I remove this, I need to refresh the page (minimum three times) to get a link to generated image from second XML.

    Do you have an idea how to do it more professionally? I can't be sure that every image will be generated in 6 seconds (sometimes shorter sometimes longer). Is there any method for genereting the result only after receiving $result_img? Thanks in advance for your help!

    According to Pho.to API, An add task request is a queued POST request.

    In my opinion, Send request in while-loop, but wait for smaller time instead of fixed 6 seconds, Check the status in image_process_response, Keep looping until it is not InProgress, After that, You can safely send second request to get processed image result.

    You may encounter timeout issue due to low timeout configuration for DoS protection if you run this script on web server (via CGI/FastCGI), To resolve this situation, You need a queue for adding task in your HTTP request, and then process it offline (means without web environment).

    评论

报告相同问题?

悬赏问题

  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法